package com.yx.cloud.adapter;

import android.content.Context;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.iot.product.sweeper.bean.Appointment;
import com.yx.cloud.R;
import java.util.List;

/**
 * 保存闹钟信息的adapter
 *
 * @version 1.0 2015/05
 */
public class AlarmClockAdapter extends BaseAdapter {

    private static final String TAG = "AlarmClockAdapter";

    private Context mContext;
    private List<Appointment> mList;
    private Callback callback;

    public AlarmClockAdapter(Context context, List<Appointment> list, Callback callback1) {
        mContext = context;
        callback = callback1;
        mList = list;
    }

    @Override
    public int getCount() {
        return mList.size();
    }

    @Override
    public Appointment getItem(int position) {
        return mList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(mContext, R.layout.layout_alarm_item, null);
            viewHolder.tvAlarmHour = convertView.findViewById(R.id.tv_alarmHour);
            viewHolder.tvAlarmMinute = convertView.findViewById(R.id.tv_alarmMinute);
            viewHolder.tvTypeMode = convertView.findViewById(R.id.tv_alarm_name);
            viewHolder.tvLoopsDesc = convertView.findViewById(R.id.tv_Alarm_desc);
            viewHolder.toggleBtn = convertView.findViewById(R.id.tb_isopen);
            viewHolder.LlAlarmExpired = convertView.findViewById(R.id.ll_alarmQueryIcon);

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        try {
            Appointment bean = mList.get(position);
            int h = bean.getTime() / 3600;
            int min = bean.getTime() / 60 % 60;
            if (h >= 10) {
                viewHolder.tvAlarmHour.setText(h + "");
            } else {
                viewHolder.tvAlarmHour.setText("0" + h);
            }
            if (min >= 10) {
                // 设定闹钟时间的显示
                viewHolder.tvAlarmMinute.setText(min + "");
            } else {
                // 设定闹钟时间的显示
                viewHolder.tvAlarmMinute.setText("0" + min);
            }


            List<Integer> period = bean.getRepeat();
            StringBuffer sb = new StringBuffer();
            //Log.d(TAG, "sb--222---->" + period.size());
            if (period != null) {
                if (period.size() == 0) {
                    sb.append(mContext.getString(R.string.MO_OnlyOnce));
                } else if (period.size() == 7) {
                    sb.append(mContext.getString(R.string.MO_Everyday));
                } else if (period.size() == 5) {
                    if (period.toString().contains("1") && period.toString().contains("2")
                            && period.toString().contains("3") && period.toString().contains("4")
                            && period.toString().contains("5")) {
                        sb.append(mContext.getString(R.string.MO_Weekday));
                    } else {
                        for (int i = 0; i < period.size(); i++) {
                            if (1 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Mon));
                            } else if (2 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Tues));
                            } else if (3 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Wed));
                            } else if (4 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Thur));
                            } else if (5 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Fri));
                            } else if (6 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Sat));
                            } else if (0 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Sun));
                            }
                            if (i < period.size() - 1) {
                                sb.append(" 、 ");
                            }
                        }
                    }

                } else {
                    if (period.size() == 2 && period.toString().contains("6") && period.toString().contains("0")) {
                        sb.append(mContext.getString(R.string.MO_Weekend));
                    } else {
                        for (int i = 0; i < period.size(); i++) {
                            if (1 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Mon));
                            } else if (2 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Tues));
                            } else if (3 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Wed));
                            } else if (4 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Thur));
                            } else if (5 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Fri));
                            } else if (6 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Sat));
                            } else if (0 == period.get(i)) {
                                sb.append(mContext.getString(R.string.MO_Sun));
                            }
                            if (i < period.size() - 1) {
                                sb.append(" 、 ");
                            }
                        }
                    }

                }
                viewHolder.tvLoopsDesc.setText(sb.toString());
            }

            boolean unlock = bean.isEnable();
            if (unlock) {
                viewHolder.toggleBtn.setChecked(true);
            } else {
                viewHolder.toggleBtn.setChecked(false);
            }

            boolean appointmentExpired = bean.isAppointmentExpired();
            Log.d("appointment", "adapter appointmentEnable is " + appointmentExpired);
            if (appointmentExpired) {
                viewHolder.toggleBtn.setVisibility(View.INVISIBLE);
                viewHolder.LlAlarmExpired.setVisibility(View.VISIBLE);
            } else {
                viewHolder.LlAlarmExpired.setVisibility(View.INVISIBLE);
                viewHolder.toggleBtn.setVisibility(View.VISIBLE);
            }

            if (bean.getCleanMode() == 0) {
                viewHolder.tvTypeMode.setText(mContext.getString(R.string.U_AutoClean));
            } else {
                viewHolder.tvTypeMode.setText(mContext.getString(R.string.U_RoomClean));
            }
            viewHolder.toggleBtn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    String str = viewHolder.tvTypeMode.getText().toString().trim();
                    callback.click(v, position, str, viewHolder.toggleBtn.isChecked());
                }
            });
            viewHolder.LlAlarmExpired.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    callback.click(v, position, null, false);
                }
            });

        } catch (ClassCastException e) {
            Log.e(TAG, "classcastexception异常 ----------");
        }

        return convertView;
    }

    public interface Callback {
        public void click(View v, int position, String str, boolean isChecked);
    }

    class ViewHolder {
        TextView tvAlarmHour;
        TextView tvAlarmMinute;
        TextView tvTypeMode;
        TextView tvLoopsDesc;
        CheckBox toggleBtn;
        LinearLayout LlAlarmExpired;
    }
}
